/**
 * Created by yangfan on 2016/10/12.
 */
define(['config/appregister',"i18n","business/personMine/services/httpServices"],function(app,i18n,httpService){
    app.controller('myCollectionCtrl', function($scope,$state) {
        $scope.i18n = i18n;
        $scope.everPageShow = [30,50,100];
        $scope.tableTitle = ["资料标题","资料分类","收藏日期","操作"];

        $scope.tableTitleArr = {
            data:["资料收藏","资料分类","收藏时间","操作"],
            thesis:["论文收藏","论文分类",'收藏时间','操作'],
            vedio:['课程收藏','课程分类','收藏时间','操作'],
            questionAnswer:['问答收藏','悬赏积分','回答','状态','收藏时间',"操作"],
            info:['头条收藏','收藏时间','浏览量','操作']
        };
        // 切换title
        $scope.isDataChecked = true;
        $scope.isThesisChecked = false;
        $scope.titleNavation = [
            {title:"资料",type:"data",typeValue:4},
            {title:"论文",type:"thesis",typeValue:5},
            {title:"课程",type:"vedio",typeValue:8},
            {title:"问答",type:"questionAnswer",typeValue:10},
            {title:"头条",type:"info",typeValue:6}
        ];
        $scope.isShowCurrent = [];
        $scope.isShowCurrent[0] = true;
        $scope.curItem =  $scope.titleNavation[0];

        //待切换加载数据
        $scope.checkTitle = function(item,index){
            $scope.isCurrent = [];
            $scope.isCurrent[0] = "bold";
            $scope.listParams.pageNo = 1;
            $scope.listParams.pageSize = 10;
            $scope.paginationConf.itemsPerPage = 10;
            $scope.paginationConf.numberOfPages = Math.ceil($scope.totalItem/$scope.listParams.pageSize);
            $scope.curItem = item ;
            $scope.listParams.pageNo = 1;
            $scope.isShowCurrent = [];
            $scope.isShowCurrent[index] = true;
            myCollect(item.typeValue);
        };

        function getNoContent(){
            if($scope.curItem.type == 'data'){
                $scope.noContent = "您暂时没有收藏的资料";
            }
            if($scope.curItem.type == 'thesis'){
                $scope.noContent = "您暂时没有收藏的论文";
            }
            if($scope.curItem.type == 'vedio'){
                $scope.noContent = "您暂时没有收藏的课程";
            }
            if($scope.curItem.type == 'questionAnswer'){
                $scope.noContent = "您暂时没有收藏的问答";
            }
            if($scope.curItem.type == 'info'){
                $scope.noContent = "您暂时没有收藏的头条";
            }
        }
        //详情页跳转
        $scope.jumpTo = function(item){
            if($scope.curItem.type == 'data'){
                $state.go('eng.techdetail',{fileId:item.fkId},{reload:true});
            }
            if($scope.curItem.type == 'thesis'){
                $state.go('eng.libraryDetail',{thesisId:item.fkId},{reload:true});
            }
            if($scope.curItem.type == 'vedio'){
                $state.go('eng.vedioDetails',{courseId:item.fkId},{reload:true});
            }
            if($scope.curItem.type == 'questionAnswer'){
                $state.go('eng.postbardeta',{postbarId:item.fkId},{reload:true});
            }
            if($scope.curItem.type == 'info'){
                $state.go('eng.newsDetail',{newsId:item.fkId},{reload:true});
            }
        };


        $scope.isCurrent = [];
        $scope.isCurrent[0] = "bold";
        $scope.listParams = {
            pageNo:1,
            pageSize:10
        };
        function getPage(){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage: $scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function () {
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                    myCollect($scope.curItem.typeValue);
                }
            };
            $scope.everPageShow = $scope.paginationConf.perPageOptions;
        }


        function myCollect (type){
            var req ={
                type:"GET",
                uri:"myCollect",
                data:{
                    type:type,
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize
                }
            };
            httpService.collectServices(req).success(function(response){
                $scope.$apply(function(){
                    $scope.curModel = response.data.list;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    getPage();
                    if($scope.totalItem == 0){
                        getNoContent();
                    }

                });
            }).error(function(){

            })
        }
        //初始化  加载资料
        myCollect($scope.titleNavation[0].typeValue);

        $scope.cancelCollection = function(item){
            var req = {
                type:"POST",
                uri:"update",
                data:{
                    fkId:item.fkId,
                    type:item.type
                }
            };
            httpService.collectServices(req).success(function(response){
                //取消成功
                myCollect (item.type)
            }).error(function(){

            })
        }

        $scope.changeItemsPerPage = function(item ,index){
            $scope.isCurrent = [];
            $scope.isCurrent[index] = "bold";
            $scope.listParams.pageNo = 1;
            $scope.listParams.pageSize = item;
            $scope.paginationConf.itemsPerPage = item;
            $scope.paginationConf.numberOfPages = Math.ceil($scope.totalItem/$scope.listParams.pageSize);
            myCollect($scope.curItem.typeValue);
        };
	})
});
